package com.cheng.example.book.advise151.chap7;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * 建议94：不能初始化泛型参数和数组
 *
 * 不能定义泛型数组
 * @param <T>
 */
public class Client94<T> {

    //因为编译器在编译时需要获得T类型，但泛型在编译期类型已经被擦除了，所有new T()和 new T[5]都会报错
//    private T t = new T();
//    private T[] tArray = new T[5];

    public T t;
    public T[] tArray;
    private List<T> list = new ArrayList<T>();


    public Client94(String className) {
        try {
            Class<?> tType = Class.forName(className);
            t = (T) tType.newInstance();
            tArray = (T[])Array.newInstance(tType, 5);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws ClassNotFoundException {
//        new Foo94<>(String.valueOf(String.class));
        Client94 foo94 = new Client94<>("java.lang.String");
        System.out.println(foo94.tArray);
//        Class c= Class.forName("java.lang.String");



    }

}
